home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2005 March / Macworld CD March 2005 - Marathon Trilogy.iso / Shareware World / iPod / iPodderX.sit / iPodderX / iPodderX.app / Contents / Resources / bencode.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2005-01-07  |  10.0 KB  |  487 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4.  
  5. def decode_int(x, f):
  6.     f += 1
  7.     newf = x.index('e', f)
  8.     
  9.     try:
  10.         n = int(x[f:newf])
  11.     except (OverflowError, ValueError):
  12.         n = long(x[f:newf])
  13.  
  14.     if x[f] == '-':
  15.         if x[f + 1] == '0':
  16.             raise ValueError
  17.         
  18.     elif x[f] == '0' and newf != f + 1:
  19.         raise ValueError
  20.     
  21.     return (n, newf + 1)
  22.  
  23.  
  24. def decode_string(x, f):
  25.     colon = x.index(':', f)
  26.     
  27.     try:
  28.         n = int(x[f:colon])
  29.     except (OverflowError, ValueError):
  30.         n = long(x[f:colon])
  31.  
  32.     if x[f] == '0' and colon != f + 1:
  33.         raise ValueError
  34.     
  35.     colon += 1
  36.     return (x[colon:colon + n], colon + n)
  37.  
  38.  
  39. def decode_list(x, f):
  40.     (r, f) = ([], f + 1)
  41.     while x[f] != 'e':
  42.         (v, f) = decode_func[x[f]](x, f)
  43.         r.append(v)
  44.     return (r, f + 1)
  45.  
  46.  
  47. def decode_dict(x, f):
  48.     (r, f) = ({ }, f + 1)
  49.     lastkey = None
  50.     while x[f] != 'e':
  51.         (k, f) = decode_string(x, f)
  52.         if lastkey >= k:
  53.             raise ValueError
  54.         
  55.         lastkey = k
  56.         (r[k], f) = decode_func[x[f]](x, f)
  57.     return (r, f + 1)
  58.  
  59. decode_func = { }
  60. decode_func['l'] = decode_list
  61. decode_func['d'] = decode_dict
  62. decode_func['i'] = decode_int
  63. decode_func['0'] = decode_string
  64. decode_func['1'] = decode_string
  65. decode_func['2'] = decode_string
  66. decode_func['3'] = decode_string
  67. decode_func['4'] = decode_string
  68. decode_func['5'] = decode_string
  69. decode_func['6'] = decode_string
  70. decode_func['7'] = decode_string
  71. decode_func['8'] = decode_string
  72. decode_func['9'] = decode_string
  73.  
  74. def bdecode(x):
  75.     
  76.     try:
  77.         (r, l) = decode_func[x[0]](x, 0)
  78.     except (IndexError, KeyError):
  79.         raise ValueError
  80.  
  81.     if l != len(x):
  82.         raise ValueError
  83.     
  84.     return r
  85.  
  86.  
  87. def test_bdecode():
  88.     
  89.     try:
  90.         bdecode('0:0:')
  91.         if not 0:
  92.             raise AssertionError
  93.     except ValueError:
  94.         pass
  95.  
  96.     
  97.     try:
  98.         bdecode('ie')
  99.         if not 0:
  100.             raise AssertionError
  101.     except ValueError:
  102.         pass
  103.  
  104.     
  105.     try:
  106.         bdecode('i341foo382e')
  107.         if not 0:
  108.             raise AssertionError
  109.     except ValueError:
  110.         pass
  111.  
  112.     if not bdecode('i4e') == 0x4L:
  113.         raise AssertionError
  114.     if not bdecode('i0e') == 0x0L:
  115.         raise AssertionError
  116.     if not bdecode('i123456789e') == 0x75BCD15L:
  117.         raise AssertionError
  118.     if not bdecode('i-10e') == -0xAL:
  119.         raise AssertionError
  120.     
  121.     try:
  122.         bdecode('i-0e')
  123.         if not 0:
  124.             raise AssertionError
  125.     except ValueError:
  126.         pass
  127.  
  128.     
  129.     try:
  130.         bdecode('i123')
  131.         if not 0:
  132.             raise AssertionError
  133.     except ValueError:
  134.         pass
  135.  
  136.     
  137.     try:
  138.         bdecode('')
  139.         if not 0:
  140.             raise AssertionError
  141.     except ValueError:
  142.         pass
  143.  
  144.     
  145.     try:
  146.         bdecode('i6easd')
  147.         if not 0:
  148.             raise AssertionError
  149.     except ValueError:
  150.         pass
  151.  
  152.     
  153.     try:
  154.         bdecode('35208734823ljdahflajhdf')
  155.         if not 0:
  156.             raise AssertionError
  157.     except ValueError:
  158.         pass
  159.  
  160.     
  161.     try:
  162.         bdecode('2:abfdjslhfld')
  163.         if not 0:
  164.             raise AssertionError
  165.     except ValueError:
  166.         pass
  167.  
  168.     if not bdecode('0:') == '':
  169.         raise AssertionError
  170.     if not bdecode('3:abc') == 'abc':
  171.         raise AssertionError
  172.     if not bdecode('10:1234567890') == '1234567890':
  173.         raise AssertionError
  174.     
  175.     try:
  176.         bdecode('02:xy')
  177.         if not 0:
  178.             raise AssertionError
  179.     except ValueError:
  180.         pass
  181.  
  182.     
  183.     try:
  184.         bdecode('l')
  185.         if not 0:
  186.             raise AssertionError
  187.     except ValueError:
  188.         pass
  189.  
  190.     if not bdecode('le') == []:
  191.         raise AssertionError
  192.     
  193.     try:
  194.         bdecode('leanfdldjfh')
  195.         if not 0:
  196.             raise AssertionError
  197.     except ValueError:
  198.         pass
  199.  
  200.     if not bdecode('l0:0:0:e') == [
  201.         '',
  202.         '',
  203.         '']:
  204.         raise AssertionError
  205.     
  206.     try:
  207.         bdecode('relwjhrlewjh')
  208.         if not 0:
  209.             raise AssertionError
  210.     except ValueError:
  211.         pass
  212.  
  213.     if not bdecode('li1ei2ei3ee') == [
  214.         1,
  215.         2,
  216.         3]:
  217.         raise AssertionError
  218.     if not bdecode('l3:asd2:xye') == [
  219.         'asd',
  220.         'xy']:
  221.         raise AssertionError
  222.     if not bdecode('ll5:Alice3:Bobeli2ei3eee') == [
  223.         [
  224.             'Alice',
  225.             'Bob'],
  226.         [
  227.             2,
  228.             3]]:
  229.         raise AssertionError
  230.     
  231.     try:
  232.         bdecode('d')
  233.         if not 0:
  234.             raise AssertionError
  235.     except ValueError:
  236.         pass
  237.  
  238.     
  239.     try:
  240.         bdecode('defoobar')
  241.         if not 0:
  242.             raise AssertionError
  243.     except ValueError:
  244.         pass
  245.  
  246.     if not bdecode('de') == { }:
  247.         raise AssertionError
  248.     if not bdecode('d3:agei25e4:eyes4:bluee') == {
  249.         'age': 25,
  250.         'eyes': 'blue' }:
  251.         raise AssertionError
  252.     if not bdecode('d8:spam.mp3d6:author5:Alice6:lengthi100000eee') == {
  253.         'spam.mp3': {
  254.             'author': 'Alice',
  255.             'length': 100000 } }:
  256.         raise AssertionError
  257.     
  258.     try:
  259.         bdecode('d3:fooe')
  260.         if not 0:
  261.             raise AssertionError
  262.     except ValueError:
  263.         pass
  264.  
  265.     
  266.     try:
  267.         bdecode('di1e0:e')
  268.         if not 0:
  269.             raise AssertionError
  270.     except ValueError:
  271.         pass
  272.  
  273.     
  274.     try:
  275.         bdecode('d1:b0:1:a0:e')
  276.         if not 0:
  277.             raise AssertionError
  278.     except ValueError:
  279.         pass
  280.  
  281.     
  282.     try:
  283.         bdecode('d1:a0:1:a0:e')
  284.         if not 0:
  285.             raise AssertionError
  286.     except ValueError:
  287.         pass
  288.  
  289.     
  290.     try:
  291.         bdecode('i03e')
  292.         if not 0:
  293.             raise AssertionError
  294.     except ValueError:
  295.         pass
  296.  
  297.     
  298.     try:
  299.         bdecode('l01:ae')
  300.         if not 0:
  301.             raise AssertionError
  302.     except ValueError:
  303.         pass
  304.  
  305.     
  306.     try:
  307.         bdecode('9999:x')
  308.         if not 0:
  309.             raise AssertionError
  310.     except ValueError:
  311.         pass
  312.  
  313.     
  314.     try:
  315.         bdecode('l0:')
  316.         if not 0:
  317.             raise AssertionError
  318.     except ValueError:
  319.         pass
  320.  
  321.     
  322.     try:
  323.         bdecode('d0:0:')
  324.         if not 0:
  325.             raise AssertionError
  326.     except ValueError:
  327.         pass
  328.  
  329.     
  330.     try:
  331.         bdecode('d0:')
  332.         if not 0:
  333.             raise AssertionError
  334.     except ValueError:
  335.         pass
  336.  
  337.     
  338.     try:
  339.         bdecode('00:')
  340.         if not 0:
  341.             raise AssertionError
  342.     except ValueError:
  343.         pass
  344.  
  345.     
  346.     try:
  347.         bdecode('l-3:e')
  348.         if not 0:
  349.             raise AssertionError
  350.     except ValueError:
  351.         pass
  352.  
  353.     
  354.     try:
  355.         bdecode('i-03e')
  356.         if not 0:
  357.             raise AssertionError
  358.     except ValueError:
  359.         pass
  360.  
  361.     bdecode('d0:i3ee')
  362.  
  363. from types import StringType, IntType, LongType, DictType, ListType, TupleType
  364.  
  365. class Bencached(object):
  366.     __slots__ = [
  367.         'bencoded']
  368.     
  369.     def __init__(self, s):
  370.         self.bencoded = s
  371.  
  372.  
  373.  
  374. def encode_bencached(x, r):
  375.     r.append(x.bencoded)
  376.  
  377.  
  378. def encode_int(x, r):
  379.     r.extend(('i', str(x), 'e'))
  380.  
  381.  
  382. def encode_string(x, r):
  383.     r.extend((str(len(x)), ':', x))
  384.  
  385.  
  386. def encode_list(x, r):
  387.     r.append('l')
  388.     for i in x:
  389.         encode_func[type(i)](i, r)
  390.     
  391.     r.append('e')
  392.  
  393.  
  394. def encode_dict(x, r):
  395.     r.append('d')
  396.     ilist = x.items()
  397.     ilist.sort()
  398.     for k, v in ilist:
  399.         r.extend((str(len(k)), ':', k))
  400.         encode_func[type(v)](v, r)
  401.     
  402.     r.append('e')
  403.  
  404. encode_func = { }
  405. encode_func[type(Bencached(0))] = encode_bencached
  406. encode_func[IntType] = encode_int
  407. encode_func[LongType] = encode_int
  408. encode_func[StringType] = encode_string
  409. encode_func[ListType] = encode_list
  410. encode_func[TupleType] = encode_list
  411. encode_func[DictType] = encode_dict
  412.  
  413. try:
  414.     from types import BooleanType
  415.     encode_func[BooleanType] = encode_int
  416. except ImportError:
  417.     pass
  418.  
  419.  
  420. def bencode(x):
  421.     r = []
  422.     encode_func[type(x)](x, r)
  423.     return ''.join(r)
  424.  
  425.  
  426. def test_bencode():
  427.     if not bencode(4) == 'i4e':
  428.         raise AssertionError
  429.     if not bencode(0) == 'i0e':
  430.         raise AssertionError
  431.     if not bencode(-10) == 'i-10e':
  432.         raise AssertionError
  433.     if not bencode(0xAB54A98CEB1F0AD2L) == 'i12345678901234567890e':
  434.         raise AssertionError
  435.     if not bencode('') == '0:':
  436.         raise AssertionError
  437.     if not bencode('abc') == '3:abc':
  438.         raise AssertionError
  439.     if not bencode('1234567890') == '10:1234567890':
  440.         raise AssertionError
  441.     if not bencode([]) == 'le':
  442.         raise AssertionError
  443.     if not bencode([
  444.         1,
  445.         2,
  446.         3]) == 'li1ei2ei3ee':
  447.         raise AssertionError
  448.     if not bencode([
  449.         [
  450.             'Alice',
  451.             'Bob'],
  452.         [
  453.             2,
  454.             3]]) == 'll5:Alice3:Bobeli2ei3eee':
  455.         raise AssertionError
  456.     if not bencode({ }) == 'de':
  457.         raise AssertionError
  458.     if not bencode({
  459.         'age': 25,
  460.         'eyes': 'blue' }) == 'd3:agei25e4:eyes4:bluee':
  461.         raise AssertionError
  462.     if not bencode({
  463.         'spam.mp3': {
  464.             'author': 'Alice',
  465.             'length': 100000 } }) == 'd8:spam.mp3d6:author5:Alice6:lengthi100000eee':
  466.         raise AssertionError
  467.     if not bencode(Bencached(bencode(3))) == 'i3e':
  468.         raise AssertionError
  469.     
  470.     try:
  471.         bencode({
  472.             1: 'foo' })
  473.     except TypeError:
  474.         return None
  475.  
  476.     if not 0:
  477.         raise AssertionError
  478.  
  479.  
  480. try:
  481.     import psyco
  482.     psyco.bind(bdecode)
  483.     psyco.bind(bencode)
  484. except ImportError:
  485.     pass
  486.  
  487.